﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace SharpOSCAR.Base
{
    /// <summary>
    /// A simple array of ProtocolData entries without any block header
    /// </summary>
    public class DataArray : ProtocolData
    {
        private LinkedList<ProtocolData> _Data;

        /// <summary>
        /// Initializes the TlvArray
        /// </summary>
        public DataArray()
        {
            _Data = new LinkedList<ProtocolData>();
        }

        /// <summary>
        /// Adds a new entry to the array
        /// </summary>
        /// <param name="value">Object to add</param>
        public void Add(ProtocolData value)
        {
            _Data.AddLast(value);
        }

        /// <summary>
        /// Removes an entry from the array
        /// </summary>
        /// <param name="value">Object to remove</param>
        public void Remove(ProtocolData value)
        {
            _Data.Remove(value);
        }

        /// <summary>
        /// Gets the number of elements in the array
        /// </summary>
        public int Count
        {
            get { return _Data.Count; }
        }

        /// <summary>
        /// Converts the data object array into a byte array
        /// </summary>
        /// <returns>Returns an array of bytes representing the data object array</returns>
        public override byte[] ToByteArray()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                foreach (ProtocolData value in _Data)
                    value.WriteToStream(ms);
                return ms.ToArray();
            }
        }
    }
}
